/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-00     YTR       the first version
 */


#define DBG_TAG "rt_uart"
#define DBG_LVL DBG_LOG

#include "main.h"

#include "all.h"

struct serial_configure uart_configs = RT_SERIAL_CONFIG_DEFAULT;
struct rt_semaphore sem4,sem5,sem6;
rt_device_t u4_dev,u5_dev,u6_dev;
rt_uint8_t u5_rx_flag = 0;
rt_uint8_t u5_rx_number = 0;

rt_err_t uart4_rx_callback(rt_device_t dev,rt_size_t size){
    char buffer;
    rt_device_read(u4_dev, 0, &buffer, 1);
    return RT_EOK;
}
rt_err_t uart5_rx_callback(rt_device_t dev,rt_size_t size){
    char buffer;
    rt_device_read(u5_dev, 0, &buffer, 1);
    switch(u5_rx_flag){
    case 0:
        switch(buffer){
        case 'h':
            u5_rx_flag = 1;
            break;
        case 'f':
            u5_rx_flag = 2;
            break;
        case 's':
            u5_rx_flag = 3;
            break;
        }
        break;
    case 1:
        if(u5_rx_number == 0 && buffer == 'a'){
            u5_rx_number++;
        }
        else if(u5_rx_number == 1 && buffer == 'n'){
            u5_rx_number++;
        }
        else if(u5_rx_number == 2 && buffer == 'd'){
            u5_rx_number++;
        }
        else if(u5_rx_number == 3 && buffer == ':'){
            u5_rx_number++;
        }
        else if(u5_rx_number == 4 && buffer >= '0' && buffer <= '4'){
            Mode_set(buffer-'0');
            u5_rx_number = 0;
            u5_rx_flag = 0;
        }
        else{
            u5_rx_flag = 0;
            u5_rx_number = 0;
        }
        break;
    }
    return RT_EOK;
}
rt_err_t uart6_rx_callback(rt_device_t dev,rt_size_t size){
    char buffer;
    rt_device_read(u6_dev, 0, &buffer, 1);
    return RT_EOK;
}
void uart4_init(void){

    u4_dev = rt_device_find("uart4");
    rt_err_t ret = 0;
    if(u4_dev == RT_NULL){
        LOG_E("rt_device_find[uart4] failed....\n");
    }
    else{
        LOG_D("rt_device_find[uart4] success\n");
        ret = rt_device_open(u4_dev, RT_DEVICE_FLAG_INT_RX);
        if(ret<0){
            LOG_E("rt_device_open[uart4] failed....\n");
        }
        else{

            rt_device_control(u4_dev, RT_DEVICE_CTRL_CONFIG, (void *)&uart_configs);

            rt_sem_init(&sem4,"u4_rx_sem4",0,RT_IPC_FLAG_FIFO);

            rt_device_set_rx_indicate(u4_dev, uart4_rx_callback);

        }
    }
}
void uart5_init(void){

    u5_dev = rt_device_find("uart5");
    rt_err_t ret = 0;
    if(u5_dev == RT_NULL){
        LOG_E("rt_device_find[uart5] failed....\n");
    }
    else{
        LOG_D("rt_device_find[uart5] success\n");
        ret = rt_device_open(u5_dev, RT_DEVICE_FLAG_INT_RX);
        if(ret<0){
            LOG_E("rt_device_open[uart5] failed....\n");
        }
        else{

            rt_device_control(u5_dev, RT_DEVICE_CTRL_CONFIG, (void *)&uart_configs);

            rt_sem_init(&sem5,"u5_rx_sem5",0,RT_IPC_FLAG_FIFO);

            rt_device_set_rx_indicate(u5_dev, uart5_rx_callback);
        }
    }
}void uart6_init(void){

    u6_dev = rt_device_find("uart6");
    rt_err_t ret = 0;
    if(u6_dev == RT_NULL){
        LOG_E("rt_device_find[uart6] failed....\n");
    }
    else{
        LOG_D("rt_device_find[uart6] success\n");
        ret = rt_device_open(u6_dev, RT_DEVICE_FLAG_INT_RX);
        if(ret<0){
            LOG_E("rt_device_open[uart6] failed....\n");
        }
        else{

            rt_device_control(u6_dev, RT_DEVICE_CTRL_CONFIG, (void *)&uart_configs);

            rt_sem_init(&sem6,"u6_rx_sem6",0,RT_IPC_FLAG_FIFO);

            rt_device_set_rx_indicate(u6_dev, uart6_rx_callback);

        }
    }
}
void uart4_tx(uint8_t * value){
    rt_device_write(u4_dev, 0, value, rt_strlen((char*)value));
}
void uart5_tx(uint8_t * value){
    rt_device_write(u5_dev, 0, value, rt_strlen((char*)value));
}
void uart6_tx(uint8_t * value){
    rt_device_write(u6_dev, 0, value, rt_strlen((char*)value));
}


